<?php
/**
 * Here is your custom functions.
 */
// 判断是否是debug 模式
if (!function_exists('get_token')) {
    function get_token(){
        $authorization = request()->header('Authorization') ?? request()->header('token');
        $token = '';
        if(!empty($authorization) && request()->header('Authorization')){
            // $token = trim(substr($authorization,'6'));
            $token = trim($authorization);
        }else{
            $token = request()->get('token');
        }
        return $token;
    }
}

// get tree
if (!function_exists('get_tree')) {
    function get_tree($data,$pid=0,$pfield='pid',$child="children",$level = 1){
        $tree=[];
        foreach($data as $k=>$v){
            if($v[$pfield] == $pid){
                $v['data_level'] = $level;
                $v[$child]=get_tree($data,$v['id'],$pfield,$child,$level + 1);
                if(empty($v[$child])){
                    unset($v[$child]);
                }
                $tree[]=$v;
            }
        }
        return $tree;
    }
}
// map to options
if (!function_exists('mapToOptions')) {
    /**
     * @desc: map 转options
     * @author: xiaohp
     * @param {*} $map
     */
    function mapToOptions($map = [])
    {
        $options = [];
        if(!empty($map)) {
            foreach($map as $value => $label) {
                $options[] = [
                    'label' => $label,
                    'value' => $value,
                ];
            }
        }
        return $options;
    }
}

if (!function_exists('get_appkey')) {
    function get_appkey(){
        $str = request()->header('Xapptool-Appkey') ?? '';
        $appkey = '';
        if(!empty($str)){
            $appkey = trim($str);
        }
        return $appkey;
    }
}
if (!function_exists('get_version')) {
    function get_version(){
        $str = request()->header('Xapptool-Version') ?? '';
        $version = '';
        if(!empty($str)){
            $version = trim($str);
        }
        return $version;
    }
}
if (!function_exists('get_fingerprint')) {
    function get_fingerprint(){
        $str = request()->header('Xapptool-Fingerprint') ?? '';
        $fingerprint = '';
        if(!empty($str)){
            // $token = trim(substr($authorization,'6'));
            $fingerprint = trim($str);
        }
        return $fingerprint;
    }
}
if (!function_exists('get_xapptool_debug')) {
    function get_xapptool_debug(){
        $flag = request()->header('Xapptool-debug') ?? 0;
        return $flag;
    }
}

